package com.siblings.loan.ui.user.fm

import android.graphics.Color
import android.os.Handler
import android.os.Looper
import android.view.LayoutInflater
import android.view.View
import androidx.core.view.GravityCompat
import androidx.drawerlayout.widget.DrawerLayout
import com.lib.base.base.BaseFragment
import com.lib.base.utils.DensityUtils
import com.lib.base.utils.TitleBarUtil
import com.siblings.loan.R
import com.siblings.loan.utils.helper.TestDataLibInvoke
import com.siblings.loan.databinding.FmUserMenuBinding
import com.siblings.loan.http.HttpHelper
import com.siblings.loan.ui.steps.act.SibStepsAct
import com.siblings.loan.ui.user.act.SibFeedbackAct
import com.siblings.loan.ui.user.act.SibVersionAct
import com.siblings.loan.utils.dialog.KefuDialog
import com.siblings.loan.utils.dialog.TipsDialog
import com.siblings.loan.utils.event.SibEventType
import com.siblings.loan.utils.helper.SharedHelper
import org.json.JSONObject

/**
 * SiblingsLoan
 * @Author wwdeng
 * @CreateTime: 2024/7/8 18:12
 * @Annotation：个人中心
 */
class SibUserMenuFm(private val drawerLayout: DrawerLayout): BaseFragment() {
    lateinit var binding: FmUserMenuBinding
    override fun getSibRootView(inflater: LayoutInflater): View {
        binding = FmUserMenuBinding.inflate(inflater)
        return binding.root
    }

    override fun initSibView() {
        rootViewBinding.containerLayout.setBackgroundColor(Color.parseColor("#00000000"))
        val paddingValue = DensityUtils.dp2px(19f)
        binding.contentLayout.setPadding(paddingValue,TitleBarUtil.getStatusBarHeight(),paddingValue,0)
    }

    override fun initSibListener() {
        //关闭
        binding.closeLayout.setOnClickListener {
            drawerLayout.closeDrawer(GravityCompat.START)
        }

        //ask us
        binding.askUsLayout.setOnClickListener {
            showSibKefuDialog("User Page")
        }

        //profile
        binding.profileLayout.setOnClickListener {
            val stepData = JSONObject(SharedHelper.getSibSPString(SharedHelper.SIB_SPKEY.SIBLINGS_PROCESS))
            if (stepData.optJSONArray("steps")!!.length() == 0) {//流程已完成 - 提示
                showSibTipsDialog()
            } else {
                //流程未完成 - 进入流程页面
                binding.closeLayout.callOnClick()
                Handler(Looper.getMainLooper()).postDelayed({
                    //上报
                    TestDataLibInvoke.sycData()
                    onIntent(SibStepsAct::class.java)
                },300)
            }
        }

        //feedback
        binding.adviceLayout.setOnClickListener {
            binding.closeLayout.callOnClick()
            Handler(Looper.getMainLooper()).postDelayed({
                onIntent(SibFeedbackAct::class.java)
            },300)
        }

        //退出登录
        binding.exitLayout.setOnClickListener {
            binding.closeLayout.callOnClick()
            Handler(Looper.getMainLooper()).postDelayed({
                onIntent(SibVersionAct::class.java)
            },300)
        }
    }

    override fun initSibData() {
    }

    /**
     * 显示客服dialog
     */
    private fun showSibKefuDialog(eventRemark: String){
        val kefuDialog = KefuDialog(context,eventRemark,object : KefuDialog.IDialogCallback{
            override fun onClick(view: View?) {
            }
        })
        kefuDialog.setCancelable(true)
        kefuDialog.setCanceledOnTouchOutside(true)
        kefuDialog.show()
    }

    /**
     * 提示
     */
    private fun showSibTipsDialog(){
        val tipsDialog = TipsDialog(context,"User Page",object : TipsDialog.IDialogCallback{
            override fun onClick(view: View?) {
                when(view!!.id){
                    R.id.btnOk -> {
                        HttpHelper.addSibEventAction(SibEventType.CLICK.SIB_CLICK_DIALOG_TIPS_OK,"User Page")
                    }
                }
            }
        })
        tipsDialog.setCancelable(true)
        tipsDialog.setCanceledOnTouchOutside(false)
        tipsDialog.show()
        tipsDialog.hideSibBtnCancel()
        tipsDialog.setTipsTitle(getString(R.string.tips))
        tipsDialog.setTips(getString(R.string.user_profile_complete_tips))
    }

    override fun onStart() {
        super.onStart()
        HttpHelper.addSibEventAction(SibEventType.PAGE.SIB_PAGE_USER_START,"")
    }

    override fun onDestroy() {
        super.onDestroy()
        HttpHelper.addSibEventAction(SibEventType.PAGE.SIB_PAGE_USER_END,"")
    }
}